//
//  VKJiFenShopDetailController.m
//  V-Learn
//
//  Created by liubo on 2019/1/9.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKJiFenShopDetailController.h"
#import "VKJiFenShopDetailCell.h"
#import "VKJiFenShopDetailPayController.h"
#import "VKJiFenShopModel.h"

@interface VKJiFenShopDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) VKJiFenShopModel *model;

@end

@implementation VKJiFenShopDetailController
- (instancetype)initWithModel:(VKJiFenShopModel *)model
{
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
//    [self setupDataArray];
    
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupUI];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 50) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 200;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self setupHeaderView];
    }
    return _tableView;
}
- (void)setupHeaderView{
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenWidth] / 3 * 2 + 120)];
    bgView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = bgView;
    UIImageView *imageView = [[UIImageView alloc]init];
    [bgView addSubview:imageView];
//    _imageView = imageView;
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Public_URL,self.model.goodsImage]] placeholderImage:[UIImage imageNamed:@"girl"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@([UIScreen cz_screenWidth] / 3 * 2));
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [bgView addSubview:titleLabel];
    //    _imageView = imageView;
    titleLabel.text = self.model.goodsName;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(imageView.mas_bottom).offset(10);
    }];
    
    UIImageView *iconView = [[UIImageView alloc]init];
    [bgView addSubview:iconView];
    iconView.clipsToBounds = YES;
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    
    iconView.image = [UIImage imageNamed:@"integral"];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.height.width.equalTo(@25);
        make.left.equalTo(titleLabel);
    }];
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    [bgView addSubview:moneyLabel];
    moneyLabel.text = self.model.goodsIntegral;
    moneyLabel.textColor = [UIColor cz_colorWithRed:253 green:79 blue:83];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconView);
        make.left.mas_equalTo(iconView.mas_right).offset(10);
    }];
    
    UIView *bgV = [[UIView alloc]init];
    [bgView addSubview:bgV];
    bgV.backgroundColor = [UIColor cz_colorWithRed:249 green:249 blue:249];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(moneyLabel.mas_bottom).offset(15);
        make.left.right.mas_equalTo(bgView);
        make.height.equalTo(@10);
    }];
    
    UILabel *detailLabel = [[UILabel alloc]init];
    [bgView addSubview:detailLabel];
    detailLabel.text = @"商品详情";
    detailLabel.textColor = [UIColor cz_colorWithRed:53 green:53 blue:53];
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(bgV.mas_bottom).offset(12);
    }];
    
    
    UILabel *leftLabel = [[UILabel alloc]init];
    [bgView addSubview:leftLabel];
    leftLabel.backgroundColor = [UIColor cz_colorWithRed:102 green:102 blue:102];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.centerY.mas_equalTo(detailLabel);
        make.width.mas_equalTo(65);
        make.right.mas_equalTo(detailLabel.mas_left).offset(-10);
    }];
    
    UILabel *rightLabel = [[UILabel alloc]init];
    [bgView addSubview:rightLabel];
    rightLabel.backgroundColor = [UIColor cz_colorWithRed:102 green:102 blue:102];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.centerY.mas_equalTo(detailLabel);
        make.width.mas_equalTo(65);
        make.left.mas_equalTo(detailLabel.mas_right).offset(10);
    }];
}
- (void)setupUI{
    
    UIView *downView = [[UIView alloc]init];
    [self.view addSubview:downView];
    downView.backgroundColor = [UIColor cz_colorWithRed:249 green:249 blue:249];
    [downView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(@0);
        make.height.equalTo(@50);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [downView addSubview:button];
    button.backgroundColor = [UIColor cz_colorWithRed:235 green:81 blue:32];
    [button setTitle:@"立即兑换" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.right.mas_equalTo(downView);
        make.width.equalTo(@120);
    }];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    [downView addSubview:moneyLabel];
    moneyLabel.text = self.model.goodsIntegral;
    moneyLabel.textColor = [UIColor cz_colorWithRed:253 green:79 blue:83];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(downView);
        make.right.mas_equalTo(button.mas_left).offset(-20);
    }];
    
    UIImageView *iconView = [[UIImageView alloc]init];
    [downView addSubview:iconView];
    iconView.image = [UIImage imageNamed:@"integral"];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(downView);
        make.right.mas_equalTo(moneyLabel.mas_left).offset(-10);
        make.height.width.equalTo(@25);
    }];
}
- (void)buttonClick{
    // goodsType 0是实体  1是代金券
    VKJiFenShopDetailPayController *payVC = [[VKJiFenShopDetailPayController alloc]initWithModel:self.model];
    [self.navigationController pushViewController:payVC animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataArray.count;
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKJiFenShopDetailCell *cell = [VKJiFenShopDetailCell cellWithTableView:tableView];
    cell.model = self.model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
